﻿using System;
using System.Linq.Expressions;
using FluentlyXml.Core.Utility;

namespace FluentlyXml.Core.DSL
{
	/// <summary> </summary>
	internal class TypeSerializationSpec<T> : ITypeSerializationSpec<T>, IInitialTypeSerializationSpec<T>
	{
		private readonly ISerializationConfig _config;

		public TypeSerializationSpec(ISerializationConfig config)
		{
			_config = config;
		}

		/// <summary> </summary>
		public ITypeSerializationSpec<T> Ignore(Expression<Func<T, object>> expression)
		{
			_config.SetIgnoredProperty(typeof(T), expression.GetProperty());
			return this;
		}

		/// <summary> </summary>
		public IInitialPropertySerializationSpec<T> Serialize(Expression<Func<T, object>> expression)
		{
			return new PropertySerializationSpec<T>(expression, _config);
		}

		/// <summary> </summary>
		public ITypeSerializationSpec<T> Using(Func<T, object> expression)
		{
			return null;
		}

		public ITypeSerializationSpec<T> SerializeAllAncestorsAsThisType()
		{
			_config.MarkTypeAsSealed(typeof (T));
			return this;
		}

		public ITypeSerializationSpec<T> IgnoreAllProperties()
		{
			var properties = typeof(T).GetPropertiesForSerialization();
			properties.ForEach(p => _config.SetIgnoredProperty(typeof(T), p));
			return this;
		}
	}
}